//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    connectedWifiName: null,
    wifiList: []
  },


  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },


  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    this.initListener()

    // 获取Wifi列表
    this.getWifiList()
    //获取已连接的Wifi信息
    this.getConnectedWifiInfo()
  },

  initListener: function() {
    var obj = this
    // 当手机Wifi断开和连接成功都会进到这个回调
    // 另外，比connectWifi.success先进入
    wx.onWifiConnected(function (res) {
      console.log(res)
      if (res.wifi['SSID'] != '') {
        // 更新页面数据
        obj.setData({
          connectedWifiName: res.wifi["SSID"]
        })
      }
    })

    // Wifi列表更新时会进入
    wx.onGetWifiList(function (res) {
      console.log("count: " + res.wifiList.length)
      // 更新页面数据
      obj.setData({
        wifiList: res.wifiList
      })
      res.wifiList.forEach(device => {
        console.log(device)
      })
    })
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  getConnectedWifiInfo: function() {
    // 修改this指向
    var obj = this
    wx.getConnectedWifi({
      success: function(res) {
        console.log(res)
        // 更新页面数据
        obj.setData({
          connectedWifiName: res.wifi["SSID"]
        })
      },
      fail: function(res) {
        console.error("getConnectedWifi:")
        console.error(res)
      },
      complete: function(res) {

      },
    })
  },

  connectWifi: function(e) {
    console.log(e)
    const ds = e.currentTarget.dataset
    const deviceName = ds.name
    const deviceId = ds.deviceId
    console.log(deviceName)

    wx.connectWifi({
      SSID: deviceName,
      BSSID: deviceId,
      password: '12345678',
      success: function(res) {
        console.log("=="+res)
        console.log(res)
        wx.showToast({
          title: '已连接到'+deviceName,
          icon: 'success',
          duration: 2000
        })
      },
      fail: function(res) {
        console.error("connectWifi:")
        console.error(res)
        wx.showToast({
          title: '连接 ' + deviceName + ' 失败，' + res.errMsg,
          icon: 'none',
          duration: 4000
        })
      },
      complete: function(res) {

      },
    })
  },

  getWifiList: function() {
    wx.startWifi({
      success: function(res) {
        wx.getWifiList({
          success: function (res) {
            
          },
          fail: function (res) {
            console.error("getWifiList:")
            console.error(res)
          },
          complete: function (res) {

          },
        })
      },
      fail: function(res) {
        console.error("startWifi:")
        console.error(res)
      },
      complete: function(res) {

      },
    })
  },
})
